/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PainelCanal2.java
 *
 * Created on 28/06/2011, 21:42:36
 */
package client;

import client.colecoes.ColecaoUsuario;
import client.entidades.Channel;
import client.entidades.Client;
import client.entidades.User;
import comum.Evento;
import comum.IMsgSource;
import java.util.ArrayList;
import javax.swing.ListModel;

/**
 *
 * @author Anderson
 */
public class PainelCanal extends PainelBase {

//    private Channel canal;;;
    private ListaUsuarios lista = new ListaUsuarios(new ArrayList<User>());

    public Channel getCanal() {
        return (Channel) parent;
    }

    @Override
    protected void atualiza(Evento evento) {

        if (evento.fonte instanceof IMsgSource && ((IMsgSource) evento.fonte) == parent) {

            if (evento.fonte instanceof Channel) {

                if (evento.arg instanceof String) {
                    String linha = (String) evento.arg;
                    this.addLinhaTexto(linha);
                    this.pisca();
                }

                if (evento.arg instanceof ColecaoUsuario) {

                    //listaUsuarios.removeAll();
                    ArrayList<User> usuarios = ((ColecaoUsuario) evento.arg).getUsuarios();
                    lista = new ListaUsuarios(usuarios);
                    listaUsuarios.setModel(lista);
                    this.pisca();
                }
            }

        }
    }

    @Override
    protected String getPainelName() {
        return getCanal().getStringName();
    }

    /** Creates new form PainelCanal2 */
    public PainelCanal(Channel canal) {
        this.setEhChannel(true);
        this.parent = canal;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        javax.swing.JTextArea conversa = this.conversa;
        jScrollPane2 = new javax.swing.JScrollPane();
        listaUsuarios = new javax.swing.JList();
        javax.swing.JTextField entrada = this.entrada;

        jScrollPane1.setViewportView(conversa);

        listaUsuarios.setModel(new ListaUsuarios(new ArrayList<User>()));
        listaUsuarios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaUsuariosMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(listaUsuarios);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 661, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(entrada, javax.swing.GroupLayout.DEFAULT_SIZE, 843, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(entrada, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void listaUsuariosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaUsuariosMouseClicked
        if (evt.getClickCount() == 2) {
            int index = listaUsuarios.locationToIndex(evt.getPoint());
            ListModel dlm = listaUsuarios.getModel();
            listaUsuarios.ensureIndexIsVisible(index);
            String nick = dlm.getElementAt(index).toString();
            if(Client.getInstance().existeUsuario(nick)){
                JanelaPrincipal.getInstancia().daFocoPainel(JanelaPrincipal.getInstancia().getPainelPeloNome(nick));
            }else{
                User user = new User(nick);
                PainelUsuario painelUsuario = new PainelUsuario(user);
                JanelaPrincipal.getInstancia().addPainel(painelUsuario);
                Client.getInstance().addUser(user);
                
            }
        }
    }//GEN-LAST:event_listaUsuariosMouseClicked
//
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listaUsuarios;
    // End of variables declaration//GEN-END:variables

    
    protected PainelBase getInstanciaPainel() {
        return (PainelBase) parent;
    }

}
